package com.yuzhi.master.locations;


import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@MappedTypes(List.class)
public class PolygonTypeHandler extends BaseTypeHandler<List<List<Double>>> {


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
                                    List<List<Double>> points, JdbcType jdbcType)
            throws SQLException {
        StringBuilder sb = new StringBuilder();

        // 遍历所有坐标点
        for (List<Double> point : points) {
            // 每个点格式: 经度 纬度
            sb.append(point.get(0)).append(" ").append(point.get(1)).append(",");
        }

        // 确保多边形闭合（首尾点相同）
        if (!points.isEmpty()) {
            List<Double> firstPoint = points.get(0);
            sb.append(firstPoint.get(0)).append(" ").append(firstPoint.get(1));
        } else {
            // 删除最后一个多余的逗号
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
            }
        }

        ps.setString(i, sb.toString());
    }

    // 其他方法实现（根据需求）
    @Override
    public List<List<Double>> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String polygon = rs.getString(columnName);
        return parsePolygon(polygon);
    }

    @Override
    public List<List<Double>> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String polygon = rs.getString(columnIndex);
        return parsePolygon(polygon);
    }

    @Override
    public List<List<Double>> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String polygon = cs.getString(columnIndex);
        return parsePolygon(polygon);
    }

    private List<List<Double>> parsePolygon(String polygon) {
        if (polygon == null || polygon.isEmpty()) {
            return null;
        }
        // 解析POLYGON((lng lat,lng lat,...))
        int start = polygon.indexOf("((");
        int end = polygon.indexOf(")");
        if (start == -1 || end == -1) {
            return null;
        }
        String pointsStr = polygon.substring(start + 2, end);
        String[] pointArr = pointsStr.split(",");
        List<List<Double>> points = new java.util.ArrayList<>();
        for (String point : pointArr) {
            String[] lngLat = point.trim().split(" ");
            if (lngLat.length == 2) {
                try {
                    double lng = Double.parseDouble(lngLat[0]);
                    double lat = Double.parseDouble(lngLat[1]);
                    List<Double> pair = new java.util.ArrayList<>();
                    pair.add(lng);
                    pair.add(lat);
                    points.add(pair);
                } catch (NumberFormatException ignored) {}
            }
        }
        return points;
    }
}
